//
//  MCTextViewController.swift
//  MCComponentPublicUI_Example
//
//  Created by 满聪 on 2019/6/25.
//  Copyright © 2019 CocoaPods. All rights reserved.
//

import UIKit
import MCComponentPublicUI

class MCTextViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
    
        textView.appendLinkString("欢迎使用")
        textView.appendLinkString("《")
        textView.appendLinkString("用户指南", withURLString: "about:from123", isShowUnderline: true)
        textView.appendLinkString( "》，《")
        textView.appendLinkString("使用说明", withURLString: "feedback:from234")
        textView.appendLinkString("》")

        textView.frame = view.frame
        self.view.addSubview(textView)
        
    }
    
    lazy var textView: UITextView = {
        let view = UITextView()
        view.font = UIFont.mc14
        view.textColor = UIColor.orange

        view.delegate = self
        view.text = ""
        view.isSelectable = true
        //        textView.dataDetectorTypes = .link
        view.isEditable = false
        view.isScrollEnabled = false
        
        return view
    }()
}

//MARK: 代理方法

extension MCTextViewController: UITextViewDelegate {
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        
        if let scheme = URL.scheme {
            switch scheme {
            case "about" :
                showAlert(tagType: "about",
                          payload: (URL as NSURL).resourceSpecifier!.removingPercentEncoding!)
            case "feedback" :
                showAlert(tagType: "feedback",
                          payload: (URL as NSURL).resourceSpecifier!.removingPercentEncoding!)
            default:
                print("这个是普通的url")
            }
        }
        
        return true
    }
    
    //显示消息
    func showAlert(tagType:String, payload:String){
        let alertController = UIAlertController(title: "检测到\(tagType)标签",
            message: payload, preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        self.present(alertController, animated: true, completion: nil)
    }

}

